<template>
    <div class="maori-product-instance-dialog-container">
        <el-dialog :title="state.dialog.title" v-model="state.dialog.isShowDialog" width="769px">
            <el-form ref="maoriProductInstanceDialogFormRef" :model="state.maoriProductInstanceForm" size="default"
                label-width="80px">
                <el-row :gutter="35">

                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
                        <el-form-item label="售价">
                            <el-row>
                                <el-col :span="16">
                                    <el-input v-model="state.maoriProductInstanceForm.price" clearable></el-input>
                                </el-col>
                                <el-col :span="8">
                                    <p style="margin: 0; line-height: 32px; text-align: center;">{{
                                        state.productInfo.priceUnit }}</p>
                                </el-col>
                            </el-row>
                        </el-form-item>
                    </el-col>

                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
                        <el-form-item :label="state.productInfo.variantCodeOne">
                            <el-row>
                                <el-col :span="16">
                                    <el-input v-model="state.maoriProductInstanceForm.variantCodeOneValue"
                                        clearable></el-input>
                                </el-col>
                            </el-row>
                        </el-form-item>
                    </el-col>

                    <div v-if="state.btTwoExist">
                        <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
                            <el-form-item :label="state.productInfo.variantCodeTwo">
                                <el-row>
                                    <el-col :span="16">
                                        <el-input v-model="state.maoriProductInstanceForm.variantCodeTwoValue"
                                            clearable></el-input>
                                    </el-col>
                                </el-row>
                            </el-form-item>
                        </el-col>
                    </div>

                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
                        <el-form-item label="占比">
                            <el-row>
                                <el-col :span="16">
                                    <el-input v-model="state.maoriProductInstanceForm.percent" clearable></el-input>
                                </el-col>
                                <el-col :span="8">
                                    <p style="margin: 0; line-height: 32px; text-align: center;">%</p>
                                </el-col>
                            </el-row>
                        </el-form-item>
                    </el-col>

                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
                        <el-form-item label="广告占比">
                            <el-row>
                                <el-col :span="16">
                                    <el-input v-model="state.maoriProductInstanceForm.advertisingPer"
                                        clearable></el-input>
                                </el-col>
                                <el-col :span="8">
                                    <p style="margin: 0; line-height: 32px; text-align: center;">%</p>
                                </el-col>
                            </el-row>
                        </el-form-item>
                    </el-col>

                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
                        <el-form-item label="折让占比">
                            <el-row>
                                <el-col :span="16">
                                    <el-input v-model="state.maoriProductInstanceForm.discountPer" clearable></el-input>
                                </el-col>
                                <el-col :span="8">
                                    <p style="margin: 0; line-height: 32px; text-align: center;">%</p>
                                </el-col>
                            </el-row>
                        </el-form-item>
                    </el-col>

                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
                        <el-form-item label="佣金占比">
                            <el-row>
                                <el-col :span="16">
                                    <el-input v-model="state.maoriProductInstanceForm.commissionPer"
                                        clearable></el-input>
                                </el-col>
                                <el-col :span="8">
                                    <p style="margin: 0; line-height: 32px; text-align: center;">%</p>
                                </el-col>
                            </el-row>
                        </el-form-item>
                    </el-col>

                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
                        <el-form-item label="采购成本">
                            <el-row>
                                <el-col :span="16">
                                    <el-input v-model="state.maoriProductInstanceForm.purchaseCost"
                                        clearable></el-input>
                                </el-col>
                                <el-col :span="8">
                                    <p style="margin: 0; line-height: 32px; text-align: center;">¥</p>
                                </el-col>
                            </el-row>
                        </el-form-item>
                    </el-col>

                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
                        <el-form-item label="产品-长">
                            <el-row>
                                <el-col :span="16">
                                    <el-input v-model="state.maoriProductInstanceForm.productLength"
                                        clearable></el-input>
                                </el-col>
                                <el-col :span="8">
                                    <p style="margin: 0; line-height: 32px; text-align: center;">{{
                                        state.productInfo.productUnit }}</p>
                                </el-col>
                            </el-row>
                        </el-form-item>
                    </el-col>

                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
                        <el-form-item label="产品-宽">
                            <el-row>
                                <el-col :span="16">
                                    <el-input v-model="state.maoriProductInstanceForm.productWidth"
                                        clearable></el-input>
                                </el-col>
                                <el-col :span="8">
                                    <p style="margin: 0; line-height: 32px; text-align: center;">{{
                                        state.productInfo.productUnit }}</p>
                                </el-col>
                            </el-row>
                        </el-form-item>
                    </el-col>

                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
                        <el-form-item label="产品-高">
                            <el-row>
                                <el-col :span="16">
                                    <el-input v-model="state.maoriProductInstanceForm.productHeight"
                                        clearable></el-input>
                                </el-col>
                                <el-col :span="8">
                                    <p style="margin: 0; line-height: 32px; text-align: center;">{{
                                        state.productInfo.productUnit }}</p>
                                </el-col>
                            </el-row>
                        </el-form-item>
                    </el-col>

                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
                        <el-form-item label="装箱量">
                            <el-row>
                                <el-col :span="16">
                                    <el-input v-model="state.maoriProductInstanceForm.outerCartonPackCount"
                                        clearable></el-input>
                                </el-col>
                                <el-col :span="8">
                                    <p style="margin: 0; line-height: 32px; text-align: center;">个</p>
                                </el-col>
                            </el-row>
                        </el-form-item>
                    </el-col>

                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
                        <el-form-item label="装箱-长">
                            <el-row>
                                <el-col :span="16">
                                    <el-input v-model="state.maoriProductInstanceForm.packetLength"
                                        clearable></el-input>
                                </el-col>
                                <el-col :span="8">
                                    <p style="margin: 0; line-height: 32px; text-align: center;">{{
                                        state.productInfo.productUnit }}</p>
                                </el-col>
                            </el-row>
                        </el-form-item>
                    </el-col>

                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
                        <el-form-item label="装箱-宽">
                            <el-row>
                                <el-col :span="16">
                                    <el-input v-model="state.maoriProductInstanceForm.packetWidth" clearable></el-input>
                                </el-col>
                                <el-col :span="8">
                                    <p style="margin: 0; line-height: 32px; text-align: center;">{{
                                        state.productInfo.productUnit }}</p>
                                </el-col>
                            </el-row>
                        </el-form-item>
                    </el-col>

                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
                        <el-form-item label="装箱-高">
                            <el-row>
                                <el-col :span="16">
                                    <el-input v-model="state.maoriProductInstanceForm.packetHeight"
                                        clearable></el-input>
                                </el-col>
                                <el-col :span="8">
                                    <p style="margin: 0; line-height: 32px; text-align: center;">{{
                                        state.productInfo.productUnit }}</p>
                                </el-col>
                            </el-row>
                        </el-form-item>
                    </el-col>

                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
                        <el-form-item label="单个产品净重">
                            <el-row>
                                <el-col :span="16">
                                    <el-input v-model="state.maoriProductInstanceForm.productWeight"
                                        clearable></el-input>
                                </el-col>
                                <el-col :span="8">
                                    <p style="margin: 0; line-height: 32px; text-align: center;">{{
                                        state.productInfo.weightUnit }}</p>
                                </el-col>
                            </el-row>
                        </el-form-item>
                    </el-col>

                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
                        <el-form-item label="单个产品包装重">
                            <el-row>
                                <el-col :span="16">
                                    <el-input v-model="state.maoriProductInstanceForm.packageWeight"
                                        clearable></el-input>
                                </el-col>
                                <el-col :span="8">
                                    <p style="margin: 0; line-height: 32px; text-align: center;">{{
                                        state.productInfo.weightUnit }}</p>
                                </el-col>
                            </el-row>
                        </el-form-item>
                    </el-col>

                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
                        <el-form-item label="整箱实重">
                            <el-row>
                                <el-col :span="16">
                                    <el-input v-model="state.maoriProductInstanceForm.packetTrueWeight"
                                        clearable></el-input>
                                </el-col>
                                <el-col :span="8">
                                    <p style="margin: 0; line-height: 32px; text-align: center;">{{
                                        state.productInfo.weightUnit }}</p>
                                </el-col>
                            </el-row>
                        </el-form-item>
                    </el-col>

                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
                        <el-form-item label="配送费">
                            <el-row>
                                <el-col :span="16">
                                    <el-input v-model="state.maoriProductInstanceForm.distributionCost"
                                        clearable></el-input>
                                </el-col>
                                <el-col :span="8">
                                    <p style="margin: 0; line-height: 32px; text-align: center;">{{
                                        state.productInfo.priceUnit }}</p>
                                </el-col>
                            </el-row>
                        </el-form-item>
                    </el-col>

                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
                        <el-form-item label="退款占比">
                            <el-row>
                                <el-col :span="16">
                                    <el-input v-model="state.maoriProductInstanceForm.refundPer" clearable></el-input>
                                </el-col>
                                <el-col :span="8">
                                    <p style="margin: 0; line-height: 32px; text-align: center;">%</p>
                                </el-col>
                            </el-row>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="onCancel" size="default">取 消</el-button>
                    <el-button type="primary" @click="addInstance" size="default">{{ state.dialog.submitTxt
                        }}</el-button>
                </span>
            </template>
        </el-dialog>
    </div>
</template>

<script setup lang="ts" name="maoriProductInstanceDialog">
import { reactive, ref } from 'vue';

// 定义子组件向父组件传值/事件
const emit = defineEmits(['addInstance']);

// 定义变量内容
const maoriProductInstanceDialogFormRef = ref();
const state = reactive({
    maoriProductInstanceForm: {
        variantCodeOneValue: '',
        variantCodeTwoValue: '',
        percent: '',
        price: '',
        purchaseCost: '',        //采购成本
        advertisingPer: '',         //广告占比
        discountPer: '',            //折让占比
        commissionPer: '',          //佣金占比
        distributionCost: '',       //配送费
        refundPer: '',              //退款占比
        productLength: '',          //产品-长
        productWidth: '',           //产品-宽
        productHeight: '',          //产品-高
        packetLength: '',            //装箱-长
        packetWidth: '',             //装箱-宽
        packetHeight: '',            //装箱-高
        outerCartonPackCount: '',   //装箱量
        productWeight: '',          //单个产品净重
        packageWeight: '',          //单个产品包装重
        packetTrueWeight: '',       //整箱实重
    },
    productInfo: {
        variantCodeOne: '变体1',
        variantCodeTwo: '变体2',
        priceUnit: '$' as any,
        productUnit: 'CM',
        weightUnit: 'KG',
    },
    btTwoExist: false,
    dialog: {
        isShowDialog: false,
        type: '',
        title: '',
        submitTxt: '',
    },
});
// 打开弹窗
const openDialog = (productInfo: CostingProduct) => {
    state.dialog.title = '新增项目单项';
    state.dialog.submitTxt = '确 定';
    console.log(productInfo);

    state.productInfo = productInfo;

    if ('' != productInfo.variantCodeTwo) {
        state.btTwoExist = true;
    }
    state.productInfo.priceUnit = productInfo.priceUnit;

    state.dialog.type = 'add';
    state.dialog.isShowDialog = true;
    clearMaoriProductInstance();
};

// 清除弹窗数据
const clearMaoriProductInstance = () => {
    state.maoriProductInstanceForm = {
        variantCodeOneValue: '',
        variantCodeTwoValue: '',
        percent: '',
        price: '',
        purchaseCost: '',
        advertisingPer: '',
        discountPer: '',
        commissionPer: '',
        distributionCost: '',
        refundPer: '',
        productLength: '',
        productWidth: '',
        productHeight: '',
        packetLength: '',
        packetWidth: '',
        packetHeight: '',
        outerCartonPackCount: '',
        productWeight: '',
        packageWeight: '',
        packetTrueWeight: '',
    }
}

// 关闭弹窗
const closeDialog = () => {
    state.dialog.isShowDialog = false;
};

// 取消
const onCancel = () => {
    closeDialog();
};

// 提交
async function addInstance() {
    closeDialog(); // 关闭弹窗
    const instance = state.maoriProductInstanceForm;
    emit('addInstance', instance);
};
// 暴露变量
defineExpose({
    openDialog,
});
</script>
